package cn.dlc.com.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.dromara.hmily.common.config.HmilyDbConfig;
import org.dromara.hmily.core.bootstrap.HmilyTransactionBootstrap;
import org.dromara.hmily.core.service.HmilyInitService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

/**
 * @author 邓立川
 * @date 2021/1/12 16:42
 * @desc 数据库配置类，这里弄了一个代理，主要是为了整合seata
 */
@Configuration
public class DatabaseConfiguration {

    @Autowired
    private Environment env;

    @Bean
    @ConfigurationProperties("spring.datasource")
    public DruidDataSource druidDataSource(){
        return new DruidDataSource();
    }

    @Bean
    public HmilyTransactionBootstrap hmilyTransactionBootstrap(HmilyInitService hmilyInitService){
        HmilyTransactionBootstrap bootstrap = new HmilyTransactionBootstrap(hmilyInitService);
        bootstrap.setSerializer(env.getProperty("org.dromara.hmily.serializer"));
        bootstrap.setRecoverDelayTime(Integer.parseInt(env.getProperty("org.dromara.hmily.recoverDelayTime")));
        bootstrap.setRetryMax(Integer.parseInt(env.getProperty("org.dromara.hmily.retryMax")));
        bootstrap.setScheduledDelay(Integer.parseInt(env.getProperty("org.dromara.hmily.scheduledDelay")));
        bootstrap.setScheduledThreadMax(Integer.parseInt(env.getProperty("org.dromara.hmily.scheduledThreadMax")));
        bootstrap.setRepositorySupport(env.getProperty("org.dromara.hmily.repositorySupport"));
        bootstrap.setStarted(Boolean.parseBoolean(env.getProperty("org.dromara.hmily.started")));

        HmilyDbConfig config = new HmilyDbConfig();
        config.setDriverClassName(env.getProperty("org.dromara.hmily.hmilyDbConfig.driverClassName"));
        config.setUrl(env.getProperty("org.dromara.hmily.hmilyDbConfig.url"));
        config.setUsername(env.getProperty("org.dromara.hmily.hmilyDbConfig.username"));
        config.setPassword(env.getProperty("org.dromara.hmily.hmilyDbConfig.password"));
        bootstrap.setHmilyDbConfig(config);

        bootstrap.setHmilyMongoConfig(null);

        return bootstrap;

    }

}
